package com.mapeditor.toolbar 
{
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	
	
	public class ToolbarPanel extends Sprite{
		
		private var _buttonsContainer:Sprite;		
		private var _buttonsContainerBackground:Sprite;		
		private var _titleContainer:Sprite;		
		private var _titleContainerBackGround:Sprite;		
		
		
		/**
		 * Calculo do proximo ponto em x para add um botao.
		 */		
		
		private var maxPoint:Number;
		private var _buttons:Array;
		private var hComponent:Number;
		private var wComponent:Number;
		
		/**
		 * construtor padrao da classe
		 */
		
		public function ToolbarPanel(title:String,h:Number){
			this.hComponent = h;
			this.wComponent = 20;
			this.title = title;
			this._buttonsContainer = new Sprite;
			this._buttonsContainerBackground = new Sprite;
			this._titleContainer = new Sprite;
			this._titleContainerBackGround = new Sprite;
			_fgTitle = (title != "");

			addChild(this._buttonsContainerBackground);
			if (_fgTitle)
			{				
				addChild(this._titleContainerBackGround);
				addChild(this._titleContainer);
			}
			addChild(this._buttonsContainer);
			
			
			
			renderTitleContainer();
			renderButtonsContainer();
			
			
			
			_buttons = new Array;
			maxPoint = 0;
		}
		
		private function renderTitleContainer():void {
			if (!_fgTitle)
				return;
			_titleContainer.graphics.clear();
			
			
			if(_titleLabel ==null)
				_titleLabel = new Label(11,0x5B5F85);
			_titleLabel.setText(title);			
			_titleLabel.reloadFormat();
			_titleContainer.addChild(_titleLabel);
			
			if (wComponent < _titleLabel.width)
				wComponent = _titleLabel.width + 5;
			else
			{
				_titleLabel.width = wComponent;
				//_titleLabel.checkFormat("center");
			}
				
			_titleLabel.x = wComponent/2 - _titleLabel.width/2;						
			
			
			renderBackground(_titleContainer.graphics, wComponent, 15);
			_titleContainer.setChildIndex(_titleLabel,_titleContainer.numChildren-1);
			_titleContainer.y = this.hComponent - _titleContainer.height;
			addChild(_titleContainer);
		}
		private function renderButtonsContainer():void {
			_buttonsContainerBackground.graphics.clear();
			renderBackground(_buttonsContainerBackground.graphics, wComponent, this.hComponent - _titleContainer.height,8,8);
			_buttonsContainerBackground.y = 2;
			
		}
		private function renderBackground(g:Graphics,w:Number,h:Number,ew:Number=0,eh:Number=0):void {
			var fType:String = GradientType.LINEAR;
			//Colors of our gradient in the form of an array
			var colors:Array = [ 0xD8DFFF, 0xF6F6F6];
			var colorLine:Array = [ 0xD8DFFF, 0xCCCCCC];
			//Store the Alpha Values in the form of an array
			var alphas:Array = [ 1, 1 ];
			//Array of color distribution ratios.
			//The value defines percentage of the width where the color is sampled at 100%
			var ratios:Array = [ 0, 255 ];
			//Create a Matrix instance and assign the Gradient Box
			var matr:Matrix = new Matrix();
			matr.createGradientBox(w, h, (Math.PI/180)*(-90), 0, 0 );
			//SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
			var sprMethod:String = SpreadMethod.REFLECT;
			g.lineStyle(1,colorLine[0]);
			g.lineGradientStyle(fType, colorLine, alphas, ratios, null, sprMethod);
			
			g.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod);				
			g.drawRoundRect(0, 0, w, h,ew,eh);
			g.endFill();
		}
		
		/************************************************************
		 * Adiciona um botao a partir de uma imagem
		 * @param	btnPath			Caminho do icone
		 * @param	nm				Nome do botao
		 * @param	next 		 	se deseja que seja posicionado ao final
		 * @param	eventMessage	Nome da mensagem a ser enviada pelo evento padrao EVENT_TOOLBAR	
		 ***********************************************************/
		
		public function addButtonImg(btnPath:String, classEvent:String = "", event:String = "",nm:String="", label:String="",hint:String="",param:Object=null,w:Number=19,h:Number=23, fgEnabled:Boolean = true):void{
			var sBtn:SESimpleButton = new SESimpleButton();
			sBtn.setContentImg(btnPath,w,h);
			this.addButton(sBtn,classEvent, event,nm,label,hint,param);
			sBtn.setEnabled(fgEnabled);
		}
		
		public function addSmallButtonImg(btnPath:String, classEvent:String = "", event:String = "",nm:String="", hint:String="",param:Object=null,w:Number=16,h:Number=16, fgEnabled:Boolean = true):void{
			var btn:SESmallButton = new SESmallButton();
			btn.setContentImg(btnPath,w,h);
			
			
			if (nm != "")
				btn.name = nm;
			_buttonsContainer.addChild(btn);
			btn.y = 5;// this._buttonsContainerBackground.height / 2 - btn.height / 2;
			_buttons.push(btn);
			
			btn.setHint(hint);
			btn.enableHintPosRef(true);
			btn.addEventListener(MouseEvent.MOUSE_DOWN, function ev1(e:Event):void{btn.mouseDown()});
			btn.addEventListener(MouseEvent.MOUSE_OUT, function ev2(e:Event):void{btn.mouseOut()});
			btn.addEventListener(MouseEvent.MOUSE_OVER, function ev3(e:Event):void{btn.mouseOver()});
			
			btn.addEventListener(MouseEvent.MOUSE_UP, function ev4(e:Event):void{_btnEvent(btn,classEvent,event,param);btn.mouseUp()});
			btn.mouseOut();
			
			
			btn.setEnabled(fgEnabled);
		}
		
		/************************************************************
		 * Adiciona um botao a partir de um SimpleButton
		 * @param	btn				SESimpleButton a ser adicionado na toolbar
		 * @param	next 		 	se deseja que seja posicionado ao final
		 * @param	nm				Nome do botao
		 * @param	eventMessage	Nome da mensagem a ser enviada pelo evento padrao EVENT_TOOLBAR	
		 ***********************************************************/
		
		public function addButton(btn:SESimpleButton,classEvent:String="", event:String="",nm:String="",label:String = "",hint:String="", param:Object=null):void{
			
			if (nm != "")
				btn.name = nm;
			_buttonsContainer.addChild(btn);
			btn.y = 5;// this._buttonsContainerBackground.height / 2 - btn.height / 2;
			_buttons.push(btn);
			
			btn.setLabel(label);
			btn.setHint(hint);
			btn.enableHintPosRef(true);
			btn.addEventListener(MouseEvent.MOUSE_DOWN, function ev1(e:Event):void{btn.mouseDown()});
			btn.addEventListener(MouseEvent.MOUSE_OUT, function ev2(e:Event):void{btn.mouseOut()});
			btn.addEventListener(MouseEvent.MOUSE_OVER, function ev3(e:Event):void{btn.mouseOver()});
			
			btn.addEventListener(MouseEvent.MOUSE_UP, function ev4(e:Event):void{_btnEvent(btn,classEvent,event,param);btn.mouseUp()});
			btn.mouseOut();
			//setChildIndex(this.hint, numChildren - 1);
			
		}//end of method
		
		public function addObject(mc:MovieClip):void {
			_buttonsContainer.addChild(mc);
			_buttons.push(mc);
		}
		
		public function outPut():void {
			
			var i:uint = 0;
			var s:uint = _buttons.length;
			var marginBeetween:uint = 2;
			for (; i < s;i++)
			{
				_buttons[i].x = maxPoint;			
				maxPoint = _buttons[i].x + _buttons[i].width + marginBeetween;				
			}
			
			this.wComponent = maxPoint;			
			
			renderTitleContainer();
			renderButtonsContainer();
			
			//centralizar os botoes
			this._buttonsContainer.x = this._buttonsContainerBackground.width * 0.5 - this._buttonsContainer.width * 0.5;
			
		}
		
		/************************************************************
		 * Redimensiona a toolbar de agordo com a largura passada por parametro
		 * @param	w		nova largura da toolbar
		 ***********************************************************/
		public function resizeToolbar(w:Number):void{
			
		}//end of method
		
		
		
		/************************************************************
		 * Retorna o botao
		 * @param	nm		nome do botao
		 ***********************************************************/
		public function getBtn(nm:String):SESimpleButton {
			var i:uint, size:uint;
			size = this._buttons.length;
			for (i = 0; i < size; i++) {
				if (_buttons[i].name == nm)
					return _buttons[i];
			}
			return null;
		}//end of method
		/************************************************************
		 * Medodo do Evento de CLICK automatico: Envia um evento do tipo AS3EventMessage
		 * @param	msg		Nome da mensagem a ser enviada pelo evento padrao EVENT_TOOLBAR	
		 ***********************************************************/
		private function _btnEvent(btn:Object,classEvent:String,event:String,param:Object):void {
			
			if(this.stage!=null)
				this.stage.focus = null;
			
			if (!btn.enabled) 
				return;
			var m:AS3EventMessage = AS3EventMessage.CreateMessage(event, "", param);
			AS3EventRouter.SendEvent(classEvent,m);
			
		}
		
		public function getWidth():Number {
			return this.wComponent;
		}
		
	}
}